home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / areuh.tar / areuh / assembler / aobj.c < prev    next >
C/C++ Source or Header  |  1990-10-10  |  5KB  |  167 lines

  1. /*
  2.  * Authors :
  3.  *   Pierre DAVID (pda@masi.ibp.fr or pda@frunip62.bitnet)
  4.  *   Janick TAILLANDIER
  5.  *
  6.  * This program can be freely used or distributed as long as this
  7.  * note is kept.
  8.  *
  9.  * This program is provided "as is".
  10.  */
  11.  
  12. /******************************************************************************
  13.  
  14.                                  AREUH ASSEMBLER
  15.  
  16.                               OBJECT FILE UTILITIES
  17.  
  18.  
  19. o_init (), o_print (), dump_linker_infos ()
  20.  
  21. ******************************************************************************/
  22.  
  23. #include "aglobal.h"
  24.  
  25. #define fputl(lg,fd)   fwrite((char *)(&(lg)),sizeof(long int),1,fd)
  26.  
  27. long int zero = 0L ;
  28. long int magic ;
  29.  
  30.  
  31. /******************************************************************************
  32.  
  33.                                     O_INIT
  34.  
  35.  
  36. synopsis : void o_init ()
  37. description : open object file and, if modular assembly, mark information for
  38.   linker (address of second part of file, and size of code in nibs).
  39.  
  40. ******************************************************************************/
  41.  
  42. void o_init ()
  43. {
  44.     uchar dfl [MAXLEN+1] ;
  45.  
  46.     if (modular) dfl_extension (dfl, fsource, "ao") ;
  47.     else strcpy (dfl, "lex") ;
  48.  
  49.     look_obj (fobject, dfl) ;                        /* ensure name validity */
  50.         
  51.     if (modular) fd_o = fopen (fobject, WAO_MODE) ;     /* untranslated mode */
  52.     else         fd_o =fopen (fobject, "w") ;           /* text mode */
  53.     if (fd_o==NULL)
  54.         error (ERROPN, fobject) ;      /* error opening file */
  55.     if (modular)
  56.     {
  57.         magic = AO_MAGIC ;
  58.         fputl (magic, fd_o) ;
  59.         fputl (zero, fd_o) ;
  60.         fputl (zero, fd_o) ;
  61.     }
  62.     else
  63.     {
  64.         magic = AL_MAGIC ;
  65.         fputl (magic, fd_o) ;
  66.     }
  67.     if (ferror (fd_o)) error (ERRWRT, fobject) ;
  68. }
  69.  
  70.  
  71. /******************************************************************************
  72.  
  73.                                    O_PRINT
  74.  
  75.  
  76. synopsis : void o_print (str, len)
  77.            uchar *str
  78.            int len
  79. description :  burp
  80.  
  81. ******************************************************************************/
  82.  
  83. void o_print (str, len)
  84. uchar *str ;
  85. int len ;
  86. {
  87.     int i ;
  88.     
  89.     if (len)
  90.     {
  91.         if (len>18) for (i=0; i<len; i++) putc ((int) '0', fd_o) ;
  92.         else        for (i=0; i<len; i++) putc ((int) str[i], fd_o) ;
  93.     }
  94.     if (ferror (fd_o)) error (ERRWRT, fobject) ;
  95. }
  96.  
  97.  
  98. /******************************************************************************
  99.  
  100.                                DUMP_LINKER_INFOS
  101.  
  102.  
  103. synopsis : void dump_linker_infos ()
  104. description : append to objet file external public definitions (symbolic or
  105.   numeric) and external references usages.
  106.  
  107. ******************************************************************************/
  108.  
  109. void dump_linker_infos ()
  110. {
  111.     int i ;                       /* index in h_label table */
  112.     struct symbol *pl ;           /* points into a label list */
  113.     long int p2, p3 ;             /* addresses of second and third parts */
  114.     long int nl = 0, nu = 0;      /* # labels (part 2), # used (part 3) */
  115.     long int l ;                  /* auxiliar */
  116.     struct xused *h ;
  117.  
  118.     p2 = ftell (fd_o) ;           /* begin of second part */
  119.     fputl (zero, fd_o) ;          /* marker for address of third part */
  120.     fputl (zero, fd_o) ;          /* number of labels in second  part */
  121.     for (i=0; i<256; i++)
  122.     {
  123.         pl = h_label[i]->s_next ;
  124.         while (pl)
  125.         {
  126.             if ((pl->s_name[0]=='=')&&(pl->s_value!=LBL_EXT)
  127.                                     &&(pl->s_value!=LBL_IVL))
  128.             {
  129.                 nl++ ;
  130.                 fprintf (fd_o, "%s\n", pl->s_name) ;
  131.                 fputl (pl->s_value, fd_o) ;
  132.                 if (pl->s_value<0L)
  133.                     fprintf (fd_o, "%s\n", pl->s_def) ;
  134.                 else fputc (pl->s_type, fd_o) ;
  135.             }
  136.             pl = pl->s_next ;
  137.         }
  138.     }
  139.     p3 = ftell (fd_o) ;           /* begin of third part */
  140.     fputl (zero, fd_o) ;          /* # used in third part */
  141.     while (headxu)
  142.     {
  143.         nu++ ;
  144.         l = (long int) headxu->u_characteristic ;
  145.         fputl (l, fd_o) ;
  146.         fputl (headxu->u_pc, fd_o) ;
  147.         fprintf (fd_o, "%s\n", headxu->u_expression) ;
  148.         h = headxu ;
  149.         headxu = headxu->u_next ;
  150.         free ((char *) h) ;
  151.     }
  152.  
  153.     fseek (fd_o, zero, 0) ;
  154.     fputl (magic, fd_o) ;
  155.     fputl (p2, fd_o) ;            /* begin of second part */
  156.     fputl (pc, fd_o) ;            /* # nibs in the code */
  157.  
  158.     fseek (fd_o, p2, 0) ;
  159.     fputl (p3, fd_o) ;            /* begin of third part */
  160.     fputl (nl, fd_o) ;            /* # labels defined in this module */
  161.  
  162.     fseek (fd_o, p3, 0) ;
  163.     fputl (nu, fd_o) ;            /* # unresolved references here */
  164.  
  165.     if (ferror (fd_o)) error (ERRWRT, fobject) ;
  166. }
  167.